<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getArticleList">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'article_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增文章 </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'article_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'article_del'" type="danger" link :icon="Delete" @click="deleteArticle(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <ArticleEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="roleManage">
import { ref, reactive } from "vue";
import { Cms } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import ArticleEdit from "@/views/cms/article/ArticleEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getArticleList, saveArticle, changeArticleStatus, delArticle } from "@/api/modules/cms";
import { userStatus } from "@/utils/dict";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { useHandleData } from "@/hooks/useHandleData";
// ProTable 实例
const proTable = ref<ProTableInstance>();
//按钮权限
const { BUTTONS } = useAuthButtons();
// 表格配置项
const columns = reactive<ColumnProps<Cms.ArticleList>[]>([
  { prop: "name", label: "标题", search: { el: "input" } },
  { prop: "cateCode", label: "分类标识", search: { el: "input" } },
  { prop: "author", label: "作者" },
  {
    prop: "img",
    label: "封面",
    render: scope => {
      return <el-image src={scope.row.img} style="width: 100px; height: 100px" />;
    }
  },
  {
    prop: "status",
    label: "状态",
    tag: true,
    enum: userStatus,
    fieldNames: { label: "label", value: "value" },
    render: scope => {
      return (
        <>
          {BUTTONS.value.article_status ? (
            <el-switch
              model-value={scope.row.status}
              active-text={scope.row.status ? "启用" : "禁用"}
              active-value={1}
              inactive-value={0}
              onClick={() => changeStatus(scope.row)}
            />
          ) : (
            <el-tag type={scope.row.status ? "success" : "danger"}>{scope.row.status ? "启用" : "禁用"}</el-tag>
          )}
        </>
      );
    }
  },
  { prop: "sort", label: "排序值" },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);
//切换角色状态
const changeStatus = async (row: Cms.ArticleList) => {
  await useHandleData(changeArticleStatus, { id: row.id, status: row.status == 1 ? 0 : 1 }, `切换【${row.name}】文章状态`);
  proTable.value?.getTableList();
};

// 删除权限资源
const deleteArticle = async (params: Cms.ArticleList) => {
  await useHandleData(delArticle, { ids: params.id }, `删除【${params.name}】文章`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof ArticleEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Cms.ArticleList> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveArticle,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
